package com.lmxdawn.api.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmxdawn.api.admin.annotation.AuthRuleAnnotation;
import com.lmxdawn.api.admin.entity.Jade;
import com.lmxdawn.api.admin.entity.Md;
import com.lmxdawn.api.admin.entity.auth.AuthRole;
import com.lmxdawn.api.admin.service.IJadeService;
import com.lmxdawn.api.admin.util.JwtUtils;
import com.lmxdawn.api.common.res.BaseResponse;
import com.lmxdawn.api.common.util.ResultVOUtils;
import com.lmxdawn.api.dnf.dto.GoldCoinPageReq;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangyubin
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/admin/dnf/jade")
public class JadeController {

    @Resource
    IJadeService jadeService;

    @Value("${game.servers}")
    String gameServer;
    /**
     * 获取列表
     */
    @GetMapping("/servers")
    public BaseResponse list(HttpServletRequest request) {
        String[] serverList = gameServer.split(",");

        List<AuthRole> currentUserRoles = JwtUtils.getCurrentUserRoles((request.getHeader("X-Adminid")));
        if(CollUtil.isNotEmpty(currentUserRoles) && currentUserRoles.stream().noneMatch(r->r.getName().equals("超级管理员"))){
            Set<String> set = currentUserRoles.stream().map(AuthRole::getServer).map(x -> StrUtil.split(x, ","))
                    .flatMap(Collection::stream).collect(Collectors.toSet());
            return ResultVOUtils.success(set.stream().map(s-> MapUtil.of("value", s)).collect(Collectors.toList()));
        }
        return ResultVOUtils.success(Arrays.stream(serverList).map(s-> MapUtil.of("value", s)).collect(Collectors.toList()));
    }

    @GetMapping("/page")
    @AuthRuleAnnotation("admin/dnf/jade/page")
    public BaseResponse page(@Valid GoldCoinPageReq pageRequest, HttpServletRequest request) {

        LambdaQueryWrapper<Jade> wrapper = Wrappers.<Jade>lambdaQuery()
                .eq(Jade::getType, pageRequest.getType())
                .orderByDesc(Jade::getCreateTime);
        List<AuthRole> currentUserRoles = JwtUtils.getCurrentUserRoles(request.getHeader("X-Adminid"));
        if(CollUtil.isNotEmpty(currentUserRoles) && currentUserRoles.stream().noneMatch(r->r.getName().equals("超级管理员"))){
            Set<String> serverList = currentUserRoles.stream().map(AuthRole::getServer).map(x -> StrUtil.split(x, ","))
                    .flatMap(Collection::stream).collect(Collectors.toSet());
            wrapper.in(Jade::getGameServer, serverList);
        }

        Page<Jade> page = Page.of(pageRequest.getPage(), pageRequest.getLimit());
        return ResultVOUtils.success(jadeService.page(page, wrapper));
    }

    @PostMapping("/save")
    @AuthRuleAnnotation("admin/dnf/jade/save")
    public BaseResponse save(@Valid @RequestBody Jade md) {
        return ResultVOUtils.success(jadeService.save(md));
    }

    @PostMapping("/edit")
    @AuthRuleAnnotation("admin/dnf/jade/edit")
    public BaseResponse edit(@Valid @RequestBody Jade md) {
        return ResultVOUtils.success(jadeService.updateById(md));
    }

    @PostMapping("/delete")
    @AuthRuleAnnotation("admin/dnf/jade/delete")
    public BaseResponse delete(@Valid @RequestBody Jade md) {
        return ResultVOUtils.success(jadeService.removeById(md));
    }
}
